﻿
CREATE PROCEDURE [dbo].[proc_Project_Delete]
	(
		@PId uniqueidentifier,
		@flag int	
	)
AS
Begin
	Declare @shl int,@StateId int,@FinanceAudit int
	If @flag=0	--成单变更时不判断状态(正常删除时 0,变更时 1)
	Begin
		Select @StateId=StateId From [Order] Where OId In(Select Top 1 OrderId From Project Where ProjectId=@PId)
		If @StateId<>2
			Return
	End

	Select @FinanceAudit = FinanceAudit From Project Where ProjectId=@PId
	If @FinanceAudit = 2	--已通过支出审核
		RETURN -1

	If @flag=0	--正常项目分析，判断是否已下发任务
	Begin
		Select @shl=Count(0) From [Project_Task] Where ProjectId=@PId
		If @shl=0
			Delete From Project Where ProjectId=@PId
		Else	--已下发任务
			RETURN -2
	End
	Else	--项目变更(连同已下发的任务一起删除)
	Begin
		Delete From Project Where ProjectId=@PId And Convert(varchar(10),CreateDate,23)=Convert(varchar(10),GetDate(),23)
		Delete From Project_Task Where ProjectId=@PId
	End
End